/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Process;

import Main.Hearts;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

/**
 *
 * @author Sess
 */
class HelloThread implements Runnable {

    ServerSocket s;
    Socket[] sv;
    Thread t;
    int Start;
    Vector PlayersList;
    ClientCommunicate CC[];
    Hearts control;
    
    HelloThread(ServerSocket server, Socket[] svr, Vector PL, Hearts control) {
        
        this.control=control;
        s = server;
        sv = svr;
        Start = 0;
        PlayersList = PL;
        CC = new ClientCommunicate[10];
        t = new Thread(this);
        System.out.println("New thread: " + t);
        t.start();

    }
    
    public void run() {
        try {
            int i = 0;
            do {
                System.out.println("Waiting for a Client");
                sv[i] = s.accept();
                CC[i] = new ClientCommunicate(sv[i], PlayersList, control);
                i++;
            } while (true);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
    
    public void Start()
    {
        int j=1;
        for (int i=0;i<10&&sv[i].isConnected();i++)
        {
            CC[i].StartGame(j);
            j++;
        }
    }
}

public class ServerProcess {
    // public static Socket ss;

    ServerSocket ss;
    Socket s[];
    Vector PlayersList;
    Hearts control;
    HelloThread ht;
    
    public ServerProcess(Hearts control) {
        this.control = control;
        try {
            ss = new ServerSocket(3200);
            s = new Socket[10];
            PlayersList = new Vector();
            ht = new HelloThread(ss, s, PlayersList, this.control);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public int StartCheck() {
        if (PlayersList.size() == 3) {
            return 1;
        } else {
            return 0;
        }
    }

    public String BuildStartMessage(String hostname) {
        String SM = "START," + hostname;
        for (int i = 0; i < PlayersList.size(); i++) {
            Vector temp = new Vector();
            temp = (Vector) PlayersList.get(i);
            SM += ",";
            SM += (String) temp.get(1);
        }
        SM += ".";
        return SM;
    }

    public int StartGame() {

        try {
            if (StartCheck() == 1) {
                System.out.println("Let the game START");
               // for (int i = 0; i <= PlayersList.size(); i++) {
                    ht.Start();
                    /*Vector temp = new Vector();
                    Socket no;
                    temp = (Vector) PlayersList.get(i);
                    no = (Socket) temp.get(0);
                    String receivedMessage, sentMessage;

                    sentMessage = BuildStartMessage(hostname);
                    System.out.print(sentMessage);
                    InputStream is = s[i].getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));

                    OutputStream os = s[i].getOutputStream();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));


                    bw.write(sentMessage);
                    bw.newLine();
                    bw.flush();*/

                    //receivedMessage = br.readLine();
                    // System.out.println("Received : " + receivedMessage);
                    
                   
               // }
                return 1;
            } else {
                return 0;
            }

        } catch (Exception e) {
            System.out.println(e.toString());
            return 0;
        }
    }
}